package com.example.filesftp.ftptransfer.util;

import lombok.extern.slf4j.Slf4j;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

@Slf4j
public class FileByteUtil {

	/*
	 * http下载
	 *
	 * @throws Exception
	 */
	public static byte[] httpDownloadByte(String httpUrl) throws Exception {
		int byteread = 0;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		URL url = null;
		try {
			url = new URL(httpUrl);
			URLConnection conn = url.openConnection();
			InputStream inStream = conn.getInputStream();
			byte[] buffer = new byte[1204];
			while ((byteread = inStream.read(buffer)) != -1) {
				out.write(buffer, 0, byteread);
			}
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			throw new Exception("Get file error", e1);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw new Exception("Get file error", e);
		}
		return out.toByteArray();
	}

	public static void httpDownloadByte(String httpUrl,String filepath) throws Exception {
		int byteread = 0;
		URL url = null;
		try {
			url = new URL(httpUrl);
			URLConnection conn = url.openConnection();
			InputStream inStream = conn.getInputStream();

			try(FileOutputStream outStrem=new FileOutputStream(filepath);){
				byte[] buffer = new byte[1204];
				while ((byteread = inStream.read(buffer)) != -1) {
					outStrem.write(buffer, 0, byteread);
				}
			}
			inStream.close();
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			throw new Exception("Get file error", e1);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw new Exception("Get file error", e);
		}
	}

	public static SSHClient getSSHClient(String remoteHost,String username,String password) throws Exception{
		SSHClient client = new SSHClient();
		client.addHostKeyVerifier(new PromiscuousVerifier());
		client.connect(remoteHost);
		client.authPassword(username, password);

		return client;
	}

	/**
	 * 推送文件
	 * @param remoteHost
	 * @param username
	 * @param password
	 * @param source
	 * @param dest
	 */
	public static void sFTPClientPut(String remoteHost,String username,String password,String source, String dest){
		SFTPClient client = null;
		SSHClient sshclient=null;
		try{
			sshclient = FileByteUtil.getSSHClient(remoteHost, username, password);
			client=sshclient.newSFTPClient();

			try {
				client.mkdirs(new File(dest).getParentFile().getAbsolutePath());
			}catch (Exception e){
				log.error(e.getLocalizedMessage(),e);
			}

			client.put(source,dest);

			System.out.println("****************"+source+" 传输成功");

		}catch (Exception ex){
			ex.getLocalizedMessage();
			log.error(ex.getLocalizedMessage(),ex);
		}finally {
			try{
				if(client != null){
					client.close();
				}
				if(sshclient != null){
					sshclient.close();
				}
			}catch (Exception e){
				e.printStackTrace();
				log.error(e.getLocalizedMessage(),e);
			}
		}
	}

	public static void deleteAllFiles(File directory) {
		if (directory.isDirectory()) {
			File[] files = directory.listFiles();
			if (files != null) {
				for (File file : files) {
					if (file.isDirectory()) {
						// 递归调用删除文件夹中的文件
						deleteAllFiles(file);
					} else {
						// 删除符合条件的文件
						boolean deleted = file.delete();
						if (deleted) {
							System.out.println("已删除文件: " + file.getAbsolutePath());
						} else {
							System.out.println("无法删除文件: " + file.getAbsolutePath());
						}
					}
				}
			}
			directory.delete();
		}
	}

}
